﻿using PaperMaster.Model;
using PaperMaster.Chess.ChessModel;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PaperMaster.Chess.PieceSpace;
using PaperMaster.Info;
using PaperMaster.Chess.Bell;

namespace PaperMaster.Chess
{
    public enum AttackOwner
    {
        None,
        Player,
        Opponent,
        Env
    }
    public interface IAttack
    {
        /// <summary>
        /// 伤害
        /// </summary>
        int dmg { get; set; }
        /// <summary>
        /// 元素类型
        /// </summary>
        ElementType type { get; set; }
        /// <summary>
        /// 打击面
        /// </summary>
        List<Vector2Int> scopeOfAttack { get; set; }
        /// <summary>
        /// 发出者
        /// </summary>
        PieceOwner owner { get; set; }
    }
    /// <summary>
    /// 攻击类，场上所有的攻击均由攻击类表示
    /// </summary>
    public class Attack : IAttack
    {
        /// <summary>
        /// 攻击的量
        /// </summary>
        public int dmg { get; set; }

        /// <summary>
        /// 攻击的类型
        /// </summary>
        public ElementType type { get; set; }

        public List<Vector2Int> scopeOfAttack { get; set; } = new List<Vector2Int>();

        public PieceOwner owner { get; set; }
    } 
}
namespace PaperMaster.Chess.PlayerCore
{
    /// <summary>
    /// 附着于玩家的数据,包括老巢血量、法力量、Buff栏等等
    /// </summary>
    public interface IPlayerCoreModel : IModel
    {
        int hp { get; set; }
        int mana { get; set; }
    }
    public class PlayerCoreModel:IPlayerCoreModel 
    {
        int _hp = 30;
        public int hp
        {
            get { return _hp; }
            set
            {
                _hp = value;
                AbstractFightInfoViewController.GetController().UpdateUI("Hp");
            }
        }
        /// <summary>
        /// 好多！仅限测试
        /// </summary>
        int _mana = 10000;
        public int mana { get
            {
                return _mana;
            }
            set
            {
                if (value < 0)
                {
                    _mana = 0;
                }
                else
                {
                    _mana = value;
                    AbstractFightInfoViewController.GetController().UpdateUI("Mana");
                }
            }
        }
    }
    /// <summary>
    /// 玩家控制器。它仅仅在战斗开始时才被生成。负责处理老巢血量变化、玩家Buff、玩家法力等等
    /// </summary>
    public class PlayerCoreController
    {
        /// <summary>
        /// 默认数据，即血量上限、法力上限等等
        /// </summary>
        IPlayerCoreModel _defaultModel;
        /// <summary>
        /// 当前数据，即当前血量、当前法力等等
        /// </summary>
        IPlayerCoreModel _model;
        IChessModel _chessModel;
        public PlayerCoreController()
        {
            _chessModel = ModelContainer.Get<IChessModel>();
            _model = ModelContainer.Get<IPlayerCoreModel>();
            _defaultModel = _chessModel.defaultPlayerModel;
            RegisterEvents();
        }
        protected virtual void UpdateUI()
        {
            AbstractFightInfoViewController.GetController().UpdateUI("Hp");
            AbstractFightInfoViewController.GetController().UpdateUI("Mana");
        }
        protected virtual void RegisterEvents()
        {
            PlayerTurnBeginEvent.Register(OnPlayerTurnBegin);
            PlayerTurnEndEvent.Register(OnPlayerTurnEnd);
            FightBeginEvent.Register(OnFightBegin);
        }
        protected virtual void UnregisterEvents()
        {
            PlayerTurnBeginEvent.Unregister(OnPlayerTurnBegin);
            PlayerTurnEndEvent.Unregister(OnPlayerTurnEnd);
            FightBeginEvent.Unregister(OnFightBegin);
        }
        ~PlayerCoreController()
        {
            UnregisterEvents();
        }
        protected virtual void OnFightBegin()
        {
            UpdateUI();
        }
        protected virtual void OnPlayerTurnBegin()
        {
            RecoverMana();
        }
        protected virtual void OnPlayerTurnEnd()
        {

        }
        #region 接口
        /// <summary>
        /// 使用法力。若仍有富余的法力可用，则会消耗法力并返回true，否则返回false
        /// </summary>
        /// <param name="mana">使用的法力量</param>
        /// <returns></returns>
        public bool UseMana(int mana)
        {
            if (mana <= _model.mana)
            {
                _model.mana -= mana;
                return true;
            }
            return false;
        }
        /// <summary>
        /// 恢复至最大法力
        /// </summary>
        public void RecoverMana()
        {
            AddMana(_defaultModel.mana - _model.mana);
        }
        /// <summary>
        /// 获得法力
        /// </summary>
        /// <param name="mana">要获得的法力量</param>
        public void AddMana(int mana)
        {
            _model.mana += mana;
        }
        /// <summary>
        /// 将法力清零
        /// </summary>
        public void ClearMana()
        {
            UseMana(_model.mana);
        }
        /// <summary>
        /// 老巢受到伤害
        /// </summary>
        /// <returns></returns>
        public void TakeDamage(IAttack atk)
        {
            _model.hp -= atk.dmg;
        }
        /// <summary>
        /// 注册一个伤害控制函数
        /// </summary>
        public void RegisterTakeDmgDecorator()
        {

        }
        /// <summary>
        /// 注销一个伤害控制函数
        /// </summary>
        public void UnregisterTakeDmgDecorator()
        {

        }
        #endregion

    }
}
